/*
 * @Author: dmyang
 * @Date:   2015-08-31 21:17:45
 * @Last Modified by:   dmyang
 * @Last Modified time: 2016-03-28 09:48:22
 */

'use strict'

import 'commonCss'



// 直接使用npm模块

require('../css/main.css')
require('../css/swiper.min.css')
require('../js/lib/swiper.min.js')

let report = require('./helpers/report')
let bar = require('./helpers/bar')
let url = require('./utils/url')
let $ajax = require('./helpers/ajax')
require('./utils/site')


function handler () {
    site.getToken(render)
}

function render(){
    site.setNav(main)
    site.header()
    site._ajax({
        type:'GET',
        url: HTTP_SERVER + 'cms/api/websites/89060613317206016',
        data:{},
        success: function (res) {
            site.footer(res)
        }
    })
}
site.render = main
function main (){
    let id = site.id?site.id:site.QueryString('id')
    let ind = site.getInd(id)
    site._ajax({
        type:'GET',
        url:HTTP_SERVER+'cms/api/pages/2/properties',
        data:{},
        success: function(res){
            site.banner('108263253079019520',{type:site.type,id:id,nav:site.nav[ind],content:'<h4>2016公司大事记</h4>',address:[
                {id:'1',name:'',content:''},
            ],companyList:[
                {id:'1',name:'',address:'',phone:'',fax:''},
            ]},setMap)
            site.menuHide()
        }
    })
}
function setMap () {

    //创建和初始化地图函数：
    function initMap(){
        createMap();//创建地图
        setMapEvent();//设置地图事件
        addMapControl();//向地图添加控件
        addMarker();//向地图中添加marker
    }

    //创建地图函数：
    function createMap(){
        var map = new BMap.Map("allmap");//在百度地图容器中创建一个地图
        var point = new BMap.Point(114.57613,33.648261);//定义一个中心点坐标
        map.centerAndZoom(point,6);//设定地图的中心点和坐标并将地图显示在地图容器中
        window.map = map;//将map变量存储在全局
    }

    //地图事件设置函数：
    function setMapEvent(){
        map.enableDragging();//启用地图拖拽事件，默认启用(可不写)
        map.enableScrollWheelZoom();//启用地图滚轮放大缩小
        map.enableDoubleClickZoom();//启用鼠标双击放大，默认启用(可不写)
        map.enableKeyboard();//启用键盘上下左右键移动地图
    }

    //地图控件添加函数：
    function addMapControl(){
        //向地图中添加缩放控件
        var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
        map.addControl(ctrl_nav);
        //向地图中添加缩略图控件
        var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
        map.addControl(ctrl_ove);
        //向地图中添加比例尺控件
        var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
        map.addControl(ctrl_sca);
    }

    //标注点数组
    var markerArr = [
        {title:"时代凌宇",content:"北京市朝阳区容创路17号时代凌宇大厦",point:"116.481921|40.02245",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"广西:时代凌宇",content:"南宁市青秀区民族大道131号航洋国际城3号楼938室<br>电话/0771_5801373<br>传真/0771_5801486",point:"108.390121|22.820109",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"福建:时代凌宇",content:"福州市台江区白马路群升白马郡7#25楼<br>电话/0591-83351399<br>传真/0591-83351399",point:"119.304124|26.066484",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"湖南:时代凌宇",content:"长沙市芙蓉区东郡华城广场B2单元1219室<br>电话：0731_89833227<br>传真：0731_89833225",point:"113.036109|28.193152",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"云南:时代凌宇",content:"昆明市盘龙区穿金路实力上筑15栋1单元101室<br>电话/0871_65165228<br>传真/0871_63121463",point:"102.76219|25.091",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"四川:时代凌宇",content:"成都市高新区环球中心洲际酒店S2007<br>电话/028_85888155",point:"104.070199|30.57258",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"湖北:时代凌宇",content:"武汉市青山区4街坊66门3号<br>电话/027_86335160<br>传真/027_86841210",point:"114.407936|30.652447",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"辽宁:时代凌宇",content:"沈阳市沈河区先农坛路15号蓝星大厦202室<br>电话/024_24123337<br>传真/024_24130351",point:"123.476246|41.790445",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"山东:时代凌宇",content:"青岛市市北区市场二路33号振业大厦1056室<br>电话/0532_82826556<br>传真/0532_82826556",point:"120.326942|36.080749",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"上海:时代凌宇",content:"上海市宝山区宝杨路555弄26号101室<br>电话/021_56120845<br>传真/021_56120849",point:"121.498937|31.406269",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"河南:时代凌宇",content:"郑州市文化北路2号航天商务大厦四楼A8室<br>电话/0371_56635911",point:"113.670143|34.817249",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
        {title:"浙江:时代凌宇",content:"杭州市余杭区南苑街道东湖南路131号204室<br>电话/0571_26260111<br>传真/0571_26280111",point:"120.316087|30.420531",isOpen:1,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}},
    ];
    //创建marker
    function addMarker(){
        for(var i=0;i<markerArr.length;i++){
            var json = markerArr[i];
            var p0 = json.point.split("|")[0];
            var p1 = json.point.split("|")[1];
            var point = new BMap.Point(p0,p1);
            var iconImg = createIcon(json.icon);
            var marker = new BMap.Marker(point,{icon:iconImg});
            var iw = createInfoWindow(i);
            var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)});
            marker.setLabel(label);
            map.addOverlay(marker);
            label.setStyle({
                borderColor:"#808080",
                color:"#333",
                cursor:"pointer"
            });

            (function(){
                var index = i;
                var _iw = createInfoWindow(i);
                var _marker = marker;
                _marker.addEventListener("click",function(){
                    this.openInfoWindow(_iw);
                });
                _iw.addEventListener("open",function(){
                    _marker.getLabel().hide();
                })
                _iw.addEventListener("close",function(){
                    _marker.getLabel().show();
                })
                label.addEventListener("click",function(){
                    _marker.openInfoWindow(_iw);
                })
                if(!!json.isOpen){
                    label.hide();
                    _marker.openInfoWindow(_iw);
                }
            })()
        }
    }
    //创建InfoWindow
    function createInfoWindow(i){
        var json = markerArr[i];
        var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>");
        return iw;
    }
    //创建一个Icon
    function createIcon(json){
        var icon = new BMap.Icon("http://api.map.baidu.com/lbsapi/createmap/images/icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
        return icon;
    }

    initMap();//创建和初始化地图
}

document.addEventListener('visibilitychange', handler)
window.addEventListener('DOMContentLoaded', handler)
window.addEventListener('resize', handler)